from apps.education.student.baseViews import *
from apps.education.teacher.dao import TeacherDao


class StudentViewSet(BaseStudentViewSet):

    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = StudentDao().findAll()
        return _list

    @action(methods=['post'], detail=False)
    def getStudentInfo(self, request):
        params = getRequestParams(self.request)
        obj = StudentDao().findOneByFilters({"account": self.request.user.id})
        if obj:
            model = self.serializer_class(obj).data
            return Response(response_success(model))
        else:
            return Response(response_action(10001, '未认证'))
    
    # 学生认证接口
    @action(methods=['post'], detail=False)
    def studentAuth(self, request):
        params = getRequestParams(request)
        if params.get('id'):
            StudentDao().update(params.get('id'), params)
        else:
            params['account'] = request.user.id
            StudentDao().create(params)
        return Response(response_action(10001, '提交成功，等待管理员审核！'))
    
    # 查看认证状态
    @action(methods=['post'], detail=False)
    def checkAuthStatus(self, request):
        params = getRequestParams(self.request)
        student = StudentDao().findOneByFilters({"account": self.request.user.id})
        teacher = TeacherDao().findOneByFilters({"account": self.request.user.id})
        if student:
            return Response(response_success({'identity': 'student'}))
        elif teacher:
            return Response(response_success({'identity': 'teacher'}))
        else:
            return Response(response_action(10001, '未认证'))
